<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>simpleresult.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_result_simpleresult.js.html">simpleresult.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A SimpleResult object that implements goog.result.Result.
<a name="line17"></a> * See below for a more detailed description.
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.result.SimpleResult&#39;);
<a name="line21"></a>goog.provide(&#39;goog.result.SimpleResult.StateError&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.debug.Error&#39;);
<a name="line24"></a>goog.require(&#39;goog.result.Result&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * A SimpleResult object is a basic implementation of the
<a name="line30"></a> * goog.result.Result interface. This could be subclassed(e.g. XHRResult)
<a name="line31"></a> * or instantiated and returned by another class as a form of result. The caller
<a name="line32"></a> * receiving the result could then attach handlers to be called when the result
<a name="line33"></a> * is resolved(success or error).
<a name="line34"></a> *
<a name="line35"></a> * @constructor
<a name="line36"></a> * @implements {goog.result.Result}
<a name="line37"></a> */
<a name="line38"></a>goog.result.SimpleResult = function() {
<a name="line39"></a>  /**
<a name="line40"></a>   * The current state of this Result.
<a name="line41"></a>   * @type {goog.result.Result.State}
<a name="line42"></a>   * @private
<a name="line43"></a>   */
<a name="line44"></a>  this.state_ = goog.result.Result.State.PENDING;
<a name="line45"></a>
<a name="line46"></a>  /**
<a name="line47"></a>   * The list of handlers to call when this Result is resolved.
<a name="line48"></a>   * @type {!Array.&lt;!function(goog.result.SimpleResult)&gt;}
<a name="line49"></a>   * @private
<a name="line50"></a>   */
<a name="line51"></a>  this.handlers_ = [];
<a name="line52"></a>
<a name="line53"></a>  // The value_ and error_ properties are initialized in the constructor to
<a name="line54"></a>  // ensure that all SimpleResult instances share the same hidden class in
<a name="line55"></a>  // modern JavaScript engines.
<a name="line56"></a>
<a name="line57"></a>  /**
<a name="line58"></a>   * The &#39;value&#39; of this Result.
<a name="line59"></a>   * @type {*}
<a name="line60"></a>   * @private
<a name="line61"></a>   */
<a name="line62"></a>  this.value_ = undefined;
<a name="line63"></a>
<a name="line64"></a>  /**
<a name="line65"></a>   * The error slug for this Result.
<a name="line66"></a>   * @type {*}
<a name="line67"></a>   * @private
<a name="line68"></a>   */
<a name="line69"></a>  this.error_ = undefined;
<a name="line70"></a>};
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Error thrown if there is an attempt to set the value or error for this result
<a name="line76"></a> * more than once.
<a name="line77"></a> *
<a name="line78"></a> * @constructor
<a name="line79"></a> * @extends {goog.debug.Error}
<a name="line80"></a> */
<a name="line81"></a>goog.result.SimpleResult.StateError = function() {
<a name="line82"></a>  goog.base(this, &#39;Multiple attempts to set the state of this Result&#39;);
<a name="line83"></a>};
<a name="line84"></a>goog.inherits(goog.result.SimpleResult.StateError, goog.debug.Error);
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/** @override */
<a name="line88"></a>goog.result.SimpleResult.prototype.getState = function() {
<a name="line89"></a>  return this.state_;
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/** @override */
<a name="line94"></a>goog.result.SimpleResult.prototype.getValue = function() {
<a name="line95"></a>  return this.value_;
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/** @override */
<a name="line100"></a>goog.result.SimpleResult.prototype.getError = function() {
<a name="line101"></a>  return this.error_;
<a name="line102"></a>};
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * Attaches handlers to be called when the value of this Result is available.
<a name="line107"></a> *
<a name="line108"></a> * @param {!function(!goog.result.SimpleResult)} handler The function
<a name="line109"></a> *     called when the value is available. The function is passed the Result
<a name="line110"></a> *     object as the only argument.
<a name="line111"></a> * @override
<a name="line112"></a> */
<a name="line113"></a>goog.result.SimpleResult.prototype.wait = function(handler) {
<a name="line114"></a>  if (this.isPending_()) {
<a name="line115"></a>    this.handlers_.push(handler);
<a name="line116"></a>  } else {
<a name="line117"></a>    handler(this);
<a name="line118"></a>  }
<a name="line119"></a>};
<a name="line120"></a>
<a name="line121"></a>
<a name="line122"></a>/**
<a name="line123"></a> * Sets the value of this Result, changing the state.
<a name="line124"></a> *
<a name="line125"></a> * @param {*} value The value to set for this Result.
<a name="line126"></a> */
<a name="line127"></a>goog.result.SimpleResult.prototype.setValue = function(value) {
<a name="line128"></a>  if (this.isPending_()) {
<a name="line129"></a>    this.value_ = value;
<a name="line130"></a>    this.state_ = goog.result.Result.State.SUCCESS;
<a name="line131"></a>    this.callHandlers_();
<a name="line132"></a>  } else if (!this.isCanceled()) {
<a name="line133"></a>    // setValue is a no-op if this Result has been canceled.
<a name="line134"></a>    throw new goog.result.SimpleResult.StateError();
<a name="line135"></a>  }
<a name="line136"></a>};
<a name="line137"></a>
<a name="line138"></a>
<a name="line139"></a>/**
<a name="line140"></a> * Sets the Result to be an error Result.
<a name="line141"></a> *
<a name="line142"></a> * @param {*=} opt_error Optional error slug to set for this Result.
<a name="line143"></a> */
<a name="line144"></a>goog.result.SimpleResult.prototype.setError = function(opt_error) {
<a name="line145"></a>  if (this.isPending_()) {
<a name="line146"></a>    this.error_ = opt_error;
<a name="line147"></a>    this.state_ = goog.result.Result.State.ERROR;
<a name="line148"></a>    this.callHandlers_();
<a name="line149"></a>  } else if (!this.isCanceled()) {
<a name="line150"></a>    // setError is a no-op if this Result has been canceled.
<a name="line151"></a>    throw new goog.result.SimpleResult.StateError();
<a name="line152"></a>  }
<a name="line153"></a>};
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * Calls the handlers registered for this Result.
<a name="line158"></a> *
<a name="line159"></a> * @private
<a name="line160"></a> */
<a name="line161"></a>goog.result.SimpleResult.prototype.callHandlers_ = function() {
<a name="line162"></a>  while (this.handlers_.length) {
<a name="line163"></a>    var callback = this.handlers_.shift();
<a name="line164"></a>    callback(this);
<a name="line165"></a>  }
<a name="line166"></a>};
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>/**
<a name="line170"></a> * @return {boolean} Whether the Result is pending.
<a name="line171"></a> * @private
<a name="line172"></a> */
<a name="line173"></a>goog.result.SimpleResult.prototype.isPending_ = function() {
<a name="line174"></a>  return this.state_ == goog.result.Result.State.PENDING;
<a name="line175"></a>};
<a name="line176"></a>
<a name="line177"></a>
<a name="line178"></a>/**
<a name="line179"></a> * Cancels the Result.
<a name="line180"></a> *
<a name="line181"></a> * @return {boolean} Whether the result was canceled. It will not be canceled if
<a name="line182"></a> *    the result was already canceled or has already resolved.
<a name="line183"></a> * @override
<a name="line184"></a> */
<a name="line185"></a>goog.result.SimpleResult.prototype.cancel = function() {
<a name="line186"></a>  // cancel is a no-op if the result has been resolved.
<a name="line187"></a>  if (this.isPending_()) {
<a name="line188"></a>    this.setError(new goog.result.Result.CancelError());
<a name="line189"></a>    return true;
<a name="line190"></a>  }
<a name="line191"></a>  return false;
<a name="line192"></a>};
<a name="line193"></a>
<a name="line194"></a>
<a name="line195"></a>/** @override */
<a name="line196"></a>goog.result.SimpleResult.prototype.isCanceled = function() {
<a name="line197"></a>  return this.state_ == goog.result.Result.State.ERROR &amp;&amp;
<a name="line198"></a>         this.error_ instanceof goog.result.Result.CancelError;
<a name="line199"></a>};
</pre>


</body>
</html>
